<template>
  <Swiper
    v-if="data"
    ref="SwiperRef"
    :slidesPerView="'auto'"
    @slideChange="onSlideChange"
    :centeredSlides="true"
    loop
    class="relative"
    autoplay
  >
    <SwiperSlide
      v-for="(item, index) in data"
      :style="{ 'background-image': `url(${item.course_ku_single_pic})` }"
      :key="index"
    >
      <div class="swiper-box" @click="swiperItemClick(item)">
        <div class="left">
          <img :src="item.course_ku_single_pic" alt="" />
          <!-- <img src="~/assets/images/swiper_bg_img.png" alt="" /> -->
          <div class="mobile-title">
            {{ item.course_ku_title }}
          </div>
        </div>
        <div class="right">
          <div class="content">
            <div class="title">{{ item.course_ku_title }}</div>
            <div class="detail">
              <div>{{ item.course_cat_title }}</div>
              <div>{{ $t("content.aviation_detail.course_id") }}: {{item.course_no}}</div>
              <div>
                {{ $t("content.aviation_detail.time") }}: {{ item.y }}-{{
                  item.m
                }}-{{ item.d }}
              </div>
              <div>
                {{ $t("content.aviation_detail.trainers_num") }}:
                {{ item.people }}
              </div>
              <div>
                {{ $t("content.aviation_detail.trainers_time") }}:
                {{ item.day }} {{ $t("unit.day") }}
              </div>
              <div>
                {{ $t("content.aviation_detail.trainers_address") }}:
                {{ item.course_address_title }}
              </div>
              <div>
                {{ $t("content.aviation_detail.trainers_teacher") }}: {{item.teacher_title}}
              </div>
            </div>
          </div>
        </div>
      </div>
    </SwiperSlide>
    <div class="swiper-control">
      <div class="prev-btn" @click="clickSwiperPrev">
        <IconArrowRight class="svg"></IconArrowRight>
      </div>
      <div class="next-btn" @click="clickSwiperNext">
        <IconArrowRight class="svg"></IconArrowRight>
      </div>
    </div>
    <div class="mobile-swiper-control">
      <div
        class="dot"
        :class="currentIndex == index ? 'active' : ''"
        v-for="(i, index) in data.length"
        :key="i"
      ></div>
    </div>
  </Swiper>
  <div v-else class="swiper-skeleton">
    <div class="pc-skeleton">
      <div class="swiper">
        <div class="swiper-inner">
          <div class="swiper-slide" v-for="i in 3">
            <Skeleton block height="100%" />
          </div>
        </div>
      </div>
    </div>
    <div class="mobile-skeleton">
      <div class="swiper">
        <div class="swiper-inner">
          <div class="swiper-slide">
            <Skeleton block height="100%" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, onMounted } from "vue";
type Props = {
  data: any[];
};
defineProps<Props>();

const swiperItemClick = (item) => {
  goto("/aviation/detail", { id: item.id });
};

const SwiperRef = ref();

const currentIndex = ref(0);

const onSlideChange = () => {
  if (SwiperRef.value) {
    currentIndex.value = SwiperRef.value.$el.swiper.realIndex;
  }
};

const clickSwiperPrev = () => {
  SwiperRef.value.$el.swiper.slidePrev();
};
const clickSwiperNext = () => {
  SwiperRef.value.$el.swiper.slideNext();
};

const resize = () => {
   SwiperRef.value && SwiperRef.value.$el.swiper.init();
};

onMounted(() => {
  window.addEventListener("resize", resize);
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", resize);
});
</script>
<style scoped lang="less">
.swiper {
  position: relative;
  padding: 20px 0;
  background-color: #fff;
  &:hover {
    .prev-btn {
      transform: translate(285px, -50%);
      opacity: 1;
    }
    .next-btn {
      opacity: 1;
      transform: translate(-325px, -50%);
    }
  }
}

.swiper-slide {
  position: relative;
  width: 1200px;
  height: 440px;
  border-radius: 24px;
  margin-right: 40px;
  background-repeat: no-repeat;
  background-size: cover;
  // background-size: 88% auto;
  // background-position: right 40%;
  overflow: hidden;
  // background-image: url("~/assets/images/swiper_bg_img.png");
  .swiper-box {
    position: relative;
    z-index: 1;
    height: 100%;
    display: flex;
    .left {
      img {
        width: 720px;
        object-fit: cover;
        height: 100%;
      }
      .mobile-title {
        display: none;
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 287px;
        padding: 82px;
        font-size: 82px;
        color: #ffffff;
        background: rgba(0, 0, 0, 0.4);
      }
    }
    .right {
      padding-right: 80px;
      position: relative;
      flex: 1;
      display: flex;
      overflow: hidden;
      .content {
        position: relative;
        z-index: 2;
        flex: 1;
        display: flex;
        flex-direction: column;
        padding-left: 40px;
        background: rgba(0, 0, 0, 0.5);
        padding-right: 70px;
        font-size: 16px;
        justify-content: center;
        color: #ffffff;
        .title {
          font-size: 27px;
          margin-bottom: 10px;
        }
      }
    }
  }

  &::before {
    position: absolute;
    content: "";
    inset: 0;
    background: inherit;
    filter: blur(15px);
  }
}

.swiper-control {
  & > div {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 80px;
    height: 80px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.8);
    border-radius: 50%;
    z-index: 9;
    overflow: hidden;
    transition: all 0.3s;
    cursor: pointer;
    :deep(.svg) {
      width: 70px;
      height: 140px;
      & * {
        fill: #fff;
        transition: all 0.3s;
      }
    }
    &.prev-btn {
      :deep(.svg) {
        transform: rotate(180deg);
      }
      left: 0;
      opacity: 0;
      transform: translate(235px, -50%);
    }
    &.next-btn {
      right: 0;
      opacity: 0;
      transform: translate(-275px, -50%);
    }
    &:hover {
      :deep(.svg) {
        * {
          fill: var(--yd-primary-color);
        }
      }
    }
  }
}

.mobile-swiper-control {
  position: absolute;
  bottom: 41px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  z-index: 1;
  gap: 41px;
  .dot {
    width: 20px;
    height: 20px;
    background: #323232;
    border-radius: 10px;
    transition: width 0.3s;
    &.active {
      width: 61px;
      height: 20px;
      background: #ffffff;
    }
  }
}

.swiper {
  &::before,
  &::after {
    content: "";
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    height: 450px;
    z-index: 2;
  }
  &::before {
    left: 0;
    border-top-right-radius: 24px;
    border-bottom-right-radius: 24px;
    width: 292px;
  }
  &::after {
    right: 0;
    width: 332px;
    border-top-left-radius: 24px;
    border-bottom-left-radius: 24px;
  }
}

@media screen and (max-width: 768px) {
  .swiper {
    padding: 0;
    &::before,
    &::after {
      display: none;
    }
  }
  .swiper-slide {
    width: 100vw;
    margin-right: 0;
    height: 1130px;
    border-radius: 0px;
    &::before {
      display: none;
    }
    .swiper-box {
      padding-right: 0;
      .left {
        width: 100%;
        img {
          width: 100%;
        }
        .mobile-title {
          display: block;
        }
      }
      .right {
        display: none;
      }
    }
  }
  .swiper-control {
    display: none;
  }
  .mobile-swiper-control {
    display: flex;
  }
}

// skeleton
.swiper-skeleton {
  .swiper {
    .swiper-inner {
      transform: translateX(-915px);
      display: flex;
    }
    &::before,
    &::after {
      display: none;
    }
  }
  .swiper-slide {
    background-image: none;
  }
}

@media screen and (max-width: 768px) {
  .swiper-skeleton {
    .swiper {
      .swiper-inner {
        transform: translateX(0);
      }
    }
  }
}
</style>
